#include "add.h"
#include "ui_add.h"
#include <QMessageBox>
#include "index.h"
#include <QtSql>

add::add(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::add)
{
    ui->setupUi(this);

    // ui->nameedit->setEnabled(false);
    // ui->orderidedit->setEnabled(false);
    // ui->passedit->setEnabled(false);
    // ui->phoneedit->setEnabled(false);
    // ui->shopnameedit->setEnabled(false);
    // ui->saleedit_2->setEnabled(false);
    // ui->timeedit->setEnabled(false);
    // ui->wayedit->setEnabled(false);
    // ui->addreedit->setEnabled(false);
    // ui->idedit->setEnabled(false);
    // ui->caixiedit->setEnabled(false);
    // ui->caimingedit->setEnabled(false);
   // ui->caixiedit_2->setEnabled(false);
}

add::~add()
{
    delete ui;
}



void add::opreation(int num)
{
    number = num;
    QSqlQuery query;
    switch(num)
    {
    case 1: //tablename = "dbo.dianpu";
        ui->nameedit->setEnabled(false);
        ui->orderidedit->setEnabled(false);
        ui->passedit->setEnabled(false);
        ui->phoneedit->setEnabled(false);
        ui->wayedit->setEnabled(false);
        ui->addreedit->setEnabled(false);
        ui->idedit->setEnabled(false);
        ui->caixiedit->setEnabled(false);
        ui->caimingedit->setEnabled(false);
        ui->label_14->setText("天");
        break;
    case 2: //tablename = "dbo.dingdan";
        ui->shopnameedit->setEnabled(false);
        ui->timeedit->setEnabled(false);
        ui->caixiedit->setEnabled(false);
        ui->caimingedit->setEnabled(false);
        ui->passedit->setEnabled(false);
        ui->caixiedit_2->setEnabled(false);
        break;
    case 3: //tablename = "dbo.guanliyuan";
        ui->nameedit->setEnabled(false);
        ui->orderidedit->setEnabled(false);
        ui->phoneedit->setEnabled(false);
        ui->shopnameedit->setEnabled(false);
        ui->saleedit_2->setEnabled(false);
        ui->timeedit->setEnabled(false);
        ui->wayedit->setEnabled(false);
        ui->addreedit->setEnabled(false);
        ui->caixiedit->setEnabled(false);
        ui->caimingedit->setEnabled(false);
        ui->caixiedit_2->setEnabled(false);
        break;
    case 4: //tablename = "dbo.kefu";
        ui->orderidedit->setEnabled(false);
        ui->passedit->setEnabled(false);
        ui->phoneedit->setEnabled(false);
        ui->saleedit_2->setEnabled(false);
        ui->timeedit->setEnabled(false);
        ui->wayedit->setEnabled(false);
        ui->addreedit->setEnabled(false);
        ui->caixiedit->setEnabled(false);
        ui->caimingedit->setEnabled(false);
        ui->caixiedit_2->setEnabled(false);
        break;
    case 5: //tablename = "dbo.sonhuoyuan";
        ui->orderidedit->setEnabled(false);
        ui->passedit->setEnabled(false);
        ui->saleedit_2->setEnabled(false);
        ui->timeedit->setEnabled(false);
        ui->wayedit->setEnabled(false);
        ui->addreedit->setEnabled(false);
        ui->caixiedit->setEnabled(false);
        ui->caimingedit->setEnabled(false);
        ui->caixiedit_2->setEnabled(false);
        break;
    case 6: //tablename = "dbo.user1";
        ui->orderidedit->setEnabled(false);
        ui->shopnameedit->setEnabled(false);
        ui->saleedit_2->setEnabled(false);
        ui->timeedit->setEnabled(false);
        ui->wayedit->setEnabled(false);
        ui->addreedit->setEnabled(false);
        ui->caixiedit->setEnabled(false);
        ui->caimingedit->setEnabled(false);
        ui->caixiedit_2->setEnabled(false);
        break;
    case 7: //tablename = "dbo.wuliu";
        ui->nameedit->setEnabled(false);
        ui->orderidedit->setEnabled(false);
        ui->passedit->setEnabled(false);
        ui->shopnameedit->setEnabled(false);
        ui->saleedit_2->setEnabled(false);
        ui->wayedit->setEnabled(false);
        ui->addreedit->setEnabled(false);
        ui->caixiedit->setEnabled(false);
        ui->caimingedit->setEnabled(false);
        ui->caixiedit_2->setEnabled(false);
        ui->label_14->setText("分钟");
        break;
    case 8: //tablename = "dbo.caisan";
        ui->orderidedit->setEnabled(false);
        ui->passedit->setEnabled(false);
        ui->phoneedit->setEnabled(false);
        ui->shopnameedit->setEnabled(false);
        ui->saleedit_2->setEnabled(false);
        ui->timeedit->setEnabled(false);
        ui->wayedit->setEnabled(false);
        ui->addreedit->setEnabled(false);
        ui->nameedit->setEnabled(false);
        ui->caixiedit_2->setEnabled(false);
        break;
    default:
        ui->nameedit->setEnabled(false);
        ui->orderidedit->setEnabled(false);
        ui->passedit->setEnabled(false);
        ui->phoneedit->setEnabled(false);
        ui->shopnameedit->setEnabled(false);
        ui->saleedit_2->setEnabled(false);
        ui->timeedit->setEnabled(false);
        ui->wayedit->setEnabled(false);
        ui->addreedit->setEnabled(false);
        ui->idedit->setEnabled(false);
        ui->caixiedit->setEnabled(false);
        ui->caimingedit->setEnabled(false);
        ui->caixiedit_2->setEnabled(false);
        break;
    }
}

QSqlRecord  add::getBack()
{
    m_back.setValue("shopname",   ui->shopnameedit->text());
    m_back.setValue("sale",    ui->saleedit_2->text());
  //  qDebug()<<"add sale"<<ui->saleedit_2->text();
    m_back.setValue("name",  ui->nameedit->text());
    m_back.setValue("name",  ui->caimingedit->text());
    m_back.setValue("orderid",ui->orderidedit->text());
    m_back.setValue("addre",    ui->addreedit->text());
    m_back.setValue("monoy",  ui->saleedit_2->text());
    m_back.setValue("time",  ui->timeedit->text());
    m_back.setValue("phone",  ui->phoneedit->text());
    m_back.setValue("way",    ui->wayedit->text());
    m_back.setValue("Password",    ui->passedit->text());
    m_back.setValue("ID",    ui->idedit->text());
    m_back.setValue("cuisine",    ui->caixiedit->text());
    qDebug()<<"add m_back"<<m_back;
    return  m_back; //以记录作为返回值

}

void add::on_pushButton_clicked(bool checked)
{
   // accept();
   //  QString qSNumberA;
   //  QString qSNumberB;
   //  QString qSNumberC;
   //  QString qSNumberD;
   //  QString qSTotalSum;
   //  int dNumberA = 0;
   //  double dNumberB = 0;
   //  double totalSum = 0;

   //  qSNumberA = ui->phoneedit->text();
   //  qSNumberB =  ui->shopnameedit->text();
   //  qSNumberC =  ui->idedit->text();
   //  qSNumberD =  ui->orderidedit->text();

   //  dNumberA = qSNumberA.toInt();
   //  dNumberB = qSNumberB.toDouble();
   // // qDebug()<<QString("%1       %2       %3         %4      %5")
   //      //            .arg(qSNumberA).arg(qSNumberB).arg(qSNumberC).arg(qSNumberD);

    QSqlQuery query;


    // query.prepare("INSERT INTO dbo.lpxx(楼盘编号,楼盘价格,楼盘类型,楼盘状态) VALUES(?,?,?,?);");
    // query.addBindValue(qSNumberA);
    // query.addBindValue(qSNumberB);
    // query.addBindValue(qSNumberD);
    // query.addBindValue(qSNumberC);




    switch(number)
    {
    case 1: //tablename = "dbo.dianpu";
        query.prepare("insert into dbo.dianpu (sale,shopname,time,mark) values (?,?,?,?)");
        query.addBindValue(ui->saleedit_2->text());
        query.addBindValue(ui->shopnameedit->text());
        query.addBindValue(ui->timeedit->text());
        query.addBindValue(ui->caixiedit_2->text());
        break;
    case 2: //tablename = "dbo.dingdan";
        query.prepare("insert into dbo.dingdan (phone,orderid,money,way,name,addre,ID) values (?,?,?,?,?,?,?)");
        //query.prepare("update dbo.dingdan set phone=?,orderid=?,money=?,way=?,name=?,addre=? where ID=?");
        query.addBindValue(ui->phoneedit->text());
        query.addBindValue(ui->orderidedit->text());
        query.addBindValue(ui->saleedit_2->text());
        query.addBindValue(ui->wayedit->text());
        query.addBindValue(ui->nameedit->text());
        query.addBindValue(ui->addreedit->text());
        query.addBindValue(ui->idedit->text());

        break;
    case 3: //tablename = "dbo.guanliyuan";
        query.prepare("insert into dbo.guanliyuan (Password,ID) values(?,?)");
        //query.prepare("update dbo.guanliyuan set Password=? where ID=?");
        query.addBindValue(ui->passedit->text());
        query.addBindValue(ui->idedit->text());
        break;
    case 4: //tablename = "dbo.kefu";
        query.prepare("insert into dbo.kefu (ID,name,shopname) values(?,?,?)");
        //query.prepare("update dbo.kefu set Name=? , Shopname=? where ID=?");
        query.addBindValue(ui->idedit->text());
        query.addBindValue(ui->nameedit->text());
        query.addBindValue(ui->shopnameedit->text());

        break;
    case 5: //tablename = "dbo.sonhuoyuan";
        query.prepare("insert into dbo.sonhuoyuan (ID,name,phone,shopname) values (?,?,?,?)");
        //query.prepare("update dbo.sonhuoyuan set name=?,phone=?,shopname=? where ID=?");
        query.addBindValue(ui->idedit->text());
        query.addBindValue(ui->nameedit->text());
        query.addBindValue(ui->phoneedit->text());
        query.addBindValue(ui->shopnameedit->text());

        break;
    case 6: //tablename = "dbo.user1";
        query.prepare("insert into dbo.user1 (ID,name,phone,Password) values (?,?,?,?)");
        //query.prepare("update dbo.user1 set name=?,phone=?,Password=? where ID=?");
        query.addBindValue(ui->idedit->text());
        query.addBindValue(ui->nameedit->text());
        query.addBindValue(ui->phoneedit->text());
        query.addBindValue(ui->passedit->text());

        break;
    case 7: //tablename = "dbo.wuliu";
        query.prepare("insert into dbo.wuliu (phone,ID,time) values (?,?,?)");
        //           //query.prepare("update dbo.wuliu set phone=?,time='?' where ID=?");
        query.addBindValue(ui->phoneedit->text());
        query.addBindValue(ui->idedit->text());
        query.addBindValue(ui->timeedit->text());

        break;
    case 8: //tablename = "dbo.caidan";
        query.prepare("insert into dbo.caidan (ID,cuisine,name) values (?,?,?)");
        //query.prepare("update dbo.caidan set cuisine=? ,name=?  where ID=?");
        query.addBindValue(ui->idedit->text());
        query.addBindValue(ui->caixiedit->text());
        query.addBindValue(ui->caimingedit->text());

        break;
    }






    //query.prepare("INSERT INTO dbo.lpxx(楼盘编号,楼盘价格,楼盘类型,楼盘状态) VALUES ("+ dNumberA +",dNumberB,qSNumberD,qSNumberC);");
    if (!query.exec())
                   QMessageBox::critical(this, "错误", "记录更新错误\n"+query.lastError().text());

    accept();
    // QMessageBox::question(this,
    //                       tr("提示"),
    //                       tr("添加成功！"),
    //                       QMessageBox::Ok | QMessageBox::Cancel,
    //                       QMessageBox::Ok);





}

